package testing.main;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class TestSys {
   // static ArrayList<Question> allQuestions;
    static BufferedReader br = new BufferedReader (new InputStreamReader(System.in));

    public static void main(String[] args) throws ClassNotFoundException, IOException {
        System.out.println("Begin");
        String inFileName;
      /*  AbstractTestingSystem tstSys = new TheTestingSystem("Test_1");
        ArrayList<Question> questions = new ArrayList<Question> ();
        ArrayList<Student> students = new ArrayList<Student> ();
        ArrayList<Administrator> admins = new ArrayList<Administrator> ();
        Question questionA = tstSys.createQuestion(1,"Who is Anton?","Teacher", new String[] {"Student","Teacher","Admin"});
        Question questionB = tstSys.createQuestion(2,"What is your name?","Artem",new String[] {"Ivan","Petro","Artem","Ira","Oleg"});
        Question questionC = tstSys.createQuestion(3,"Is it November?","No",new String[] {"Yes","No","I don't know...","What?!"});
        Question questionD = tstSys.createQuestion(4,"Who is Yanuk?","President",new String[] {"Actor","DJ","President","Football-player"});
        Question questionE = tstSys.createQuestion(5,"What can I do?","Nothing",new String[] {"Nothing","Everything","Something","What?!"});
        Question questionF = tstSys.createQuestion(6,"Tested question 1","1",new String[] {"1","2","3","4"});
        Question questionG = tstSys.createQuestion(7,"Tested question 2","2",new String[] {"1","2","3","4"});
        Question questionH = tstSys.createQuestion(8,"Tested question 3","3",new String[] {"1","2","3","4"});

        Student studA = tstSys.createStudent(1,"Artem","Zelinka","za4em16","IS-72",4);
        Student studB = tstSys.createStudent(2,"Vanya","Lappo","1111","IS-72",4);
        Student studC = tstSys.createStudent(3,"Petro","Pechkin","qwerty","IS-02",1);
        Student studD = tstSys.createStudent(4,"Anya","Popova","1234","IA-83",3);
        Student studE = tstSys.createStudent(5,"Karl","Mut","iu18054","IO-91",2);
        //Administrator adminA = tstSys.createAdministrator(1,"Anton","Kraievoy","123321");
        questions.add(questionA);
        questions.add(questionB);
        questions.add(questionC);
        questions.add(questionD);
        questions.add(questionE);
        questions.add(questionF);
        questions.add(questionG);
        questions.add(questionH);
        students.add(studA);
        students.add(studB);
        students.add(studC);
        students.add(studD);
        students.add(studE);
       // admins.add(adminA);
        //System.out.println(students.getName);


       QuestionDao dao1 = new QuestionDao();
        StudentDao dao2 = new StudentDao();
        AdministratorDao dao3 = new AdministratorDao();
        for (int i =0; i <questions.size(); i++)
            {
                try {
                   // Question ques = new Question(i,questionSerialize.questions.)
                    dao1.createQuestion(questions.get(i));
                } catch (SQLException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
            }
        for (int i =0; i <students.size(); i++)
            {
                try {
                   // Question ques = new Question(i,questionSerialize.questions.)
                    dao2.createStudent(students.get(i));
                } catch (SQLException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
            }  
      /*  for (int i =0; i <admins.size(); i++)
            {
                try {
                   // Question ques = new Question(i,questionSerialize.questions.)
                    dao3.createAdministrator(admins.get(i));
                } catch (SQLException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
            }*/
      //  ArrayList<Question> questions = new ArrayList<Question> ();

    }
}
       /* System.out.println("Press 1 to read the file of questions...");
        System.out.println("Press 2 to read the file of administrators...");
        System.out.println("Press another to read the file of students...");
        String helper = br.readLine();
        if (helper.equals("1"))
        {
        QuestionAdd questionSerialize = null;
        System.out.println("Write name of the file :");
            inFileName = br.readLine();
            QuestionSerializer serialize = new QuestionSerializer();
            StringBuilder strBuild = new StringBuilder();
            try {
                BufferedReader in = new BufferedReader(new FileReader(inFileName));
                String str;
                while ((str = in.readLine()) != null)
                {
                    strBuild.append(str).append("\n");
                }
                in.close();
                questionSerialize = serialize.serializeQuestion(strBuild.toString());
            }    //Catching my exception
            catch (FileProblemException e)
            {
                System.out.println(e.str);
                e.printStackTrace();
            }
            catch (IOException e)
            {
                System.out.println(e);
            }
            catch (Exception e)
            {
                System.out.println(e);
            }
            if (questionSerialize != null) {
                System.out.println(questionSerialize.questions.toString());
            }
            //AbstractTestingSystem tstSys = new TheTestingSystem("Test_1");
            //ArrayList<Question> questions = new ArrayList<Question> ();

            QuestionDao dao1 = new QuestionDao();
            for (int i =0; i <questionSerialize.questions.size(); i++)
            {
                try {
                   // Question ques = new Question(i,questionSerialize.questions.)
                    dao1.createQuestion(questionSerialize.questions.get(i));
                } catch (SQLException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
            }
//            questionSerialize = new QuestionAdd(questions);

            QuestionSerializer serializer = new QuestionSerializer();
            FileWriter writer = null;
            try
            {
                try {
                   // Question ques = new Question(i,questionSerialize.questions.)
                    questionSerialize.questions.addAll(dao1.getAllQuestions());
                     } catch (SQLException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
                //questionSerialize.questions.addAll(dao1.getAllQuestions());
                System.out.println("Write the name of file........");
                String helperForWrite = br.readLine();
                writer = new FileWriter(helperForWrite);
                serializer.serializeAll(questionSerialize,writer);
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        } else if(helper.equals("2"))
        {
        AdministratorAdd adminSerialize = null;
        System.out.println("Write name of the file :");
            inFileName = br.readLine();
            AdministratorSerializer serializeAdm = new AdministratorSerializer();
            StringBuilder strBuild2 = new StringBuilder();
            try {
                BufferedReader in = new BufferedReader(new FileReader(inFileName));
                String str;
                while ((str = in.readLine()) != null)
                {
                    strBuild2.append(str).append("\n");
                }
                in.close();
                adminSerialize = serializeAdm.serializeAdmin(strBuild2.toString());
            }    //Catching my exception
            catch (FileProblemException e)
            {
                System.out.println(e.str);
                e.printStackTrace();
            }
            catch (IOException e)
            {
                System.out.println(e);
            }
            catch (Exception e)
            {
                System.out.println(e);
            }
            if (adminSerialize != null) {
                System.out.println(adminSerialize.admins.toString());
            }
//            questionSerialize = new QuestionAdd(questions);

            AdministratorSerializer serializerAdm = new AdministratorSerializer();
            FileWriter writerAdm = null;
            try
            {
                System.out.println("Write the name of file........");
                String helperForWrite = br.readLine();
                writerAdm = new FileWriter(helperForWrite);
                serializerAdm.serializeAll(adminSerialize,writerAdm);
                writerAdm.close();
            } catch (IOException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        }
        else
        {
        StudentAdd studentSerialize = null;
        System.out.println("Write name of the file :");
            inFileName = br.readLine();
            StudentSerializer serializeStud = new StudentSerializer();
            StringBuilder strBuild2 = new StringBuilder();
            try {
                BufferedReader in = new BufferedReader(new FileReader(inFileName));
                String str;
                while ((str = in.readLine()) != null)
                {
                    strBuild2.append(str).append("\n");
                }
                in.close();
                studentSerialize = serializeStud.serializeStud(strBuild2.toString());
            }    //Catching my exception
            catch (FileProblemException e)
            {
                System.out.println(e.str);
                e.printStackTrace();
            }
            catch (IOException e)
            {
                System.out.println(e);
            }
            catch (Exception e)
            {
                System.out.println(e);
            }
            if (studentSerialize != null) {
                System.out.println(studentSerialize.students.toString());
            }
//            questionSerialize = new QuestionAdd(questions);

            StudentSerializer serializerStud = new StudentSerializer();
            FileWriter writerStud = null;
            try
            {
                System.out.println("Write the name of file........");
                String helperForWrite = br.readLine();
                writerStud = new FileWriter(helperForWrite);
                serializerStud.serializeAll(studentSerialize,writerStud);
                writerStud.close();
            } catch (IOException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        }
    }
}
   // static Scanner input = new Scanner(System.in);
/*
    ArrayList<Question> questions;
    ArrayList<Test> tests;

    public TestSys( ArrayList<Question> questions,ArrayList<Test> tests)  {
        this.questions = questions;
        this.tests = tests;
    }
    public static void main(String[] args) throws ClassNotFoundException, IOException {
        //Properties dbProps = new Properties();
      //  dbProps.load(TestSys.class.getResourceAsStream("/testing/domain/db.properties"));
        Class.forName("org.h2.Driver");
        Connection conn = null;
        Statement stat = null;
        ResultSet resultSet = null;
        ResultSet resultSet2 = null;
        PreparedStatement prepStat;
        boolean success = false;
        try {
         //   conn1 = DriverManager.getConnection(dbProps.getProperty("url"), "sa", "");
        //    conn1 = DataBaseConnectionPool.getConnection();
        //    stat1 = conn1.createStatement();
              conn = DataBaseConnectionPool.getConnection();
           stat = conn.createStatement();
                        resultSet = stat.executeQuery("select * from QUESTION");
                        resultSet2 = stat.executeQuery("select * from TEST");
                        int colCount;
            colCount = resultSet.getMetaData().getColumnCount();
            int colCount2 = resultSet2.getMetaData().getColumnCount();
                        for (int col = 1; col <= colCount; col ++) {
                            System.out.println("col: " + col + " " + resultSet.getMetaData().getColumnName(col));
                        }

                        while (resultSet.next()) {
                            System.out.println(resultSet.getRow() + " " + resultSet.getString("NAME")+ " | "+ resultSet.getString("RIGHTANSWER"));
                        }

        /////////////////////////////
        System.out.println("Begin");
        String inFileName;
        System.out.println("Press 1 to read the file...");
        String helper = br.readLine();
        TestSys tSystem=null;
        if (helper.equals("1"))
        {
            System.out.println("Write name of the file :");
            inFileName = br.readLine();
            YAMLSerialiser yamlSerialiser = new YAMLSerialiser();
            StringBuilder strBuild = new StringBuilder();
            try {
                BufferedReader in = new BufferedReader(new FileReader(inFileName));
                String str;
                while ((str = in.readLine()) != null)
                {
                    strBuild.append(str).append("\n");
                }
                in.close();
                tSystem = yamlSerialiser.deserialiseAll(strBuild.toString());
            }    //Catching my exception
            catch (FileProblemException e)
            {
                System.out.println(e.str);
                e.printStackTrace();
            }
            catch (IOException e)
            {
                System.out.println(e);
            }
            catch (Exception e)
            {
                System.out.println(e);
            }
            if (tSystem != null) {
                System.out.println(tSystem.questions.toString());
                System.out.println(tSystem.tests.toString());
                System.out.println(tSystem.questions.get(1).getName());
                ////////////////////////////////////////////////
                int helpForBD = colCount;
                for (int j = 0; j < tSystem.questions.size(); j++){
                    helpForBD++;
                    prepStat = conn.prepareStatement("INSERT INTO QUESTION (QUESTION_ID, NAME, ANSWERS, RIGHTANSWER) VALUES ( ?, ?, ?, ?);");
                    prepStat.setInt(1, helpForBD);
                    prepStat.setString(2, tSystem.questions.get(j).getName());
                    prepStat.setString(3, Arrays.toString(tSystem.questions.get(j).getAnswers()));
                    prepStat.setString(4, tSystem.questions.get(j).getRightAnswer());
                    int psUc = prepStat.executeUpdate();
                    System.out.println("psUc = " + psUc);
                    success = true;
                }
                for (int j = 0; j < tSystem.tests.size(); j++){
                    colCount2++;
                    prepStat = conn.prepareStatement("INSERT INTO TEST (TEST_ID, NAME) VALUES ( ?, ?);");
                    prepStat.setInt(1, colCount2);
                    prepStat.setString(2, tSystem.tests.get(j).getName());
                    int psUc = prepStat.executeUpdate();
                    System.out.println("psUc = " + psUc);
                    success = true;
                }
            ////////////////////////////////////////////////
            }
        }
        else
        {
            AbstractTestingSystem tstSys = new TheTestingSystem("Test_1");
            ArrayList<Question> questions = new ArrayList<Question> ();
            Question questionA = tstSys.createQuestion("Who is Anton?","Teacher", new String[] {"Student","Teacher","Admin"});
            Question questionB = tstSys.createQuestion("What is your name?","Artem",new String[] {"Ivan","Petro","Artem","Ira","Oleg"});
            Question questionC = tstSys.createQuestion("Is it November?","Yes",new String[] {"Yes","No","I don't know...","What?!"});
            Question questionD = tstSys.createQuestion("Who is Yanuk?","President",new String[] {"Actor","DJ","President","Football-player"});
            questions.add(questionA);
            questions.add(questionB);
            questions.add(questionC);
            questions.add(questionD);
            //////////
            resultSet = stat.executeQuery("select * from QUESTION");

              while (resultSet.next()) {
                  String q = resultSet.getString("ANSWERS");
                  String[]  qq = q.trim().split(", ");
                            Question ques = new Question(resultSet.getString("NAME"),resultSet.getString("RIGHTANSWER"), qq);
                  questions.add(ques);
                        }
            //////////
            ArrayList<Test> tests = new ArrayList<Test> ();
            Test testA = tstSys.createTest("Test_English");
            Test testB = tstSys.createTest("Test_Matan");
            Test testC = tstSys.createTest("Test_Ecology");
            Test testD = tstSys.createTest("Test_MMDO");
            Test testE = tstSys.createTest("Test_TCPP");
            tests.add(testA);
            tests.add(testB);
            tests.add(testC);
            tests.add(testD);
            tests.add(testE);
            ///////////////////
             resultSet = stat.executeQuery("select * from TEST");

              while (resultSet.next()) {
                            Test tt = new Test(resultSet.getString("NAME"));
                  tests.add(tt);
                        }
            ///////////////////
            tSystem = new TestSys(questions,tests);

            YAMLSerialiser yamlSerialiser = new YAMLSerialiser();
            FileWriter writer = null;
            try
            {
                System.out.println("Write the name of file........");
                String helperForWrite = br.readLine();
                writer = new FileWriter(helperForWrite);
                yamlSerialiser.serializeAll(tSystem,writer);
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
            Student StudFirst = new Student();
            System.out.println("To take locale Italian press 1 else take locale English");
            String taker=br.readLine();
            ////////////////////////////////////////////////
           /* prepStat = conn.prepareStatement("INSERT INTO QUESTION (QUESTION_ID, NAME, ANSWERS, RIGHTANSWER) VALUES (4, ?, ?, ?);");

                        prepStat.setString(1, questionA.getName());
                        prepStat.setString(2, Arrays.toString(questionA.getAnswers()));
                        prepStat.setString(3, questionA.getRightAnswer());
            int psUc = prepStat.executeUpdate();
            System.out.println("psUc = " + psUc);

            success = true;
            */////////////////////////////////////////////////
         /*   if (taker.equals("1")){
                StudFirst.studPrint1();
                questionA.questionOut1();
                questionA.setStudAnswer(br.readLine());
                questionA.getAnswer1();
                questionB.questionOut1();
                questionB.setStudAnswer(br.readLine());
                questionB.getAnswer1();
                questionC.questionOut1();
                questionC.setStudAnswer(br.readLine());
                questionC.getAnswer1();
            }
            else
            {
                StudFirst.studPrint2();
                questionA.questionOut2();
                questionA.setStudAnswer(br.readLine());
                questionA.getAnswer2();
                questionB.questionOut2();
                questionB.setStudAnswer(br.readLine());
                questionB.getAnswer2();
                questionC.questionOut2();
                questionC.setStudAnswer(br.readLine());
                questionC.getAnswer2();
            }
        }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DataBaseConnectionPool.silentClose(stat);
            DataBaseConnectionPool.silentClose(conn);
            /*if (conn != null) {
                try {
                    if (success) {
                        conn.commit();
                    } else {
                        conn.rollback();
                    }
                    //DataBaseConnectionPool.silentClose(stat);
                    //DataBaseConnectionPool.silentClose(conn);
                    //conn.close();
                } catch (SQLException e) {
                    //  ignored
                }
            }
        }
    }
       /* System.out.println("**********************\nTo choose the Admin area press 1" +
                "\nTo choose the testing.person.Student area press 2" +
                "\nPress another key to Exit");
        i = input.nextInt();

        switch (i)
		{
		    case 1:
			    testing.person.Administrator TytFirst = new testing.person.Administrator ();
			    System.out.print("Input name: ");
                TytFirst.name = input.next();
                if (TytFirst.chackPass() == true)
			    {
				    System.out.println("***********The name : "+TytFirst.name+
                            "\n*********** Welcome! ************\n\n" +
                            "************* 1. Show the Tests ***\n" +
                            "************* 2. Exit *************");

			    }
			    else
			    {
                    System.out.println("***********The name : "+TytFirst.name
                            +"\n***********not correct password! System turned off...");
			    }
                break;
		    case 2:
			    testing.person.Student StudFirst = new testing.person.Student();
                StudFirst.studPrint();
                questionA.questionOut();
                questionA.getStudAnswer() = br.readLine();
                questionA.getAnswer();
                questionB.questionOut();
                questionB.getStudAnswer() = br.readLine();
                questionB.getAnswer();
                questionC.questionOut();
                questionC.getStudAnswer() = br.readLine();
                questionC.getAnswer();
                break;
		    default:
		    	System.out.println("***********What are you doing?");break;
		}

}

*/